﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using FrameWork.DataBaseProvider;
using System.Configuration;

namespace IndexService
{
    partial class RebuiltRealtimeService : ServiceBase
    {

        //日志类
        private Framework.Log.ILog log = Framework.Log.LogFactory.CreateInstance();
        //   words.ApplicationClass wordApp;
        private double AlarmTimeSpan;
        private System.Timers.Timer realTimer;

        private void ParseConfig()
        {
            //get the config information from app.config
            this.AlarmTimeSpan = 1000 * double.Parse(ConfigurationSettings.AppSettings["AlarmTimeSpan"].ToString().Trim());
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                ParseConfig();
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "实时库重建索引启动！"));
                this.realTimer = new System.Timers.Timer(this.AlarmTimeSpan);
                this.realTimer.Enabled = true;
                this.realTimer.Elapsed += new System.Timers.ElapsedEventHandler(RealTimer_Elapsed);
                this.realTimer.Start();
            }
            catch (Exception ex)
            {
                log.WirteLog(ex.Message);
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "实时库重建索被终止！"));
                this.realTimer.Close();
            }
        }

        protected override void OnStop()
        {
            try
            {
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "实时库重建索引停止！"));
                this.realTimer.Stop();
                this.realTimer.Enabled = false;
            }
            catch (Exception ex)
            {
                log.WirteLog(ex.Message);
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "实时库重建索引被终止！"));
            }
            finally
            {
                this.realTimer.Close();
            }
        }

        protected void RealTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                this.realTimer.Stop();
                log.WirteLog(string.Concat("\r\n", DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "操作开始...."));
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库重建索引【构建xml数据开始】......"));

                //判断实时库中，文件是否有isXML状态为1（准备创建索引的数据）
                int dataCount = IndexCore.PreapareData(EnumServices.REAL_TIME_REBUILT);

                //if (wordApp != null)//再关闭
                //{
                //    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                //    wordApp = null;
                //}


                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库重建【构建xml数据结束】,共构建数据数：", dataCount, "条"));

                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库重建【创建索引开始】......"));
                //判断实时库中，文件是否有isXML状态为1（准备创建索引的数据）
                //创建索引，创建完成一个，update一条记录的状态
                string updateIndexSql = "";
                int indexCount = 0;
                if (dataCount <= 0)
                {
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库重建【创建索引结束（无新增数据）】"));
                    return;
                }

                updateIndexSql = IndexCore.CreateIndex(ref indexCount, EnumServices.REAL_TIME_REBUILT);
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库重建【创建索引结束】,共更新记录数：", indexCount, "条"));

                if (updateIndexSql != "")
                {
                    bool result = false;
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库更新isXML状态为2【更新数据库开始】......"));
                    result = new DbHelper(Const.REAL_TIME_DBCONNECTION).ExeMoreDB(updateIndexSql);
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 实时库更新isXML状态为2【更新数据库结束】更新", result ? "成功" : "失败"));
                }

            }
            catch (Exception ex)
            {
                log.WirteLog(ex.Message);
            }
            finally
            {
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "操作结束....\r\n"));
                this.realTimer.Start();
            }
        }
    }
}
